package com.qfedu.hotelssm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.qfedu.hotelssm.dao.CtOrderdetailDao;
import com.qfedu.hotelssm.dto.CtOrderDetailDto;
import com.qfedu.hotelssm.entity.CtOrderDetail;
import com.qfedu.hotelssm.service.intf.CtOrderDetailService;
import com.qfedu.hotelssm.util.StringUtil;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author dzl
 * @date 2022/8/29 14:16
 */
@Service
public class CtOrderDetailServiceImpl implements CtOrderDetailService {
    @Autowired
    private CtOrderdetailDao dao;
    @Override
    public R save(CtOrderDetail orderDetail) {
        if (dao.save(orderDetail)>0){
            return R.ok("恭喜你，新增成功了");
        }else{
            return R.fail("亲，新增失败了");
        }

    }

    @Override
    public R del(int id) {
        if (dao.del(id)>0){
            return R.ok("恭喜你，删除成功了");
        }else{
            return R.fail("亲，删除失败了");
        }
    }

    @Override
    public PageBean page(String userid, int page, int limit) {
       // int id=new OrderDetail().getId();
        if (StringUtil.isNotEmpty(userid)){
            userid="%"+userid+"%";
        }
        int start= (page-1)*limit;
        return new PageBean((int) dao.selectCount(userid),dao.selectPage(userid,start,limit));
    }

    @Override
    public R update(CtOrderDetail orderDetail) {
        if (dao.update(orderDetail)>0){
            return R.ok("恭喜你，修改成功了");
        }else{
            return R.fail("亲，修改失败了");
        }
    }

    //导出
    @Override
    public List<CtOrderDetailDto> all() {
        return dao.all();
    }

    //导入
    @Override
    public R upload(MultipartFile file) throws IOException {
        //1.接收文件， 获取文件内容
        //1.验证上传文件是否存在
        if (!file.isEmpty()){
            //2.解析Excel -EasyExcel
            //2.解析Excel数据
         List<CtOrderDetail> list= EasyExcel.read(file.getInputStream(), CtOrderDetail.class, new ReadListener() {
             @Override
             public void invoke(Object o, AnalysisContext analysisContext) {

             }
             @Override
             public void doAfterAllAnalysed(AnalysisContext analysisContext) {

             }
         }).sheet().doReadSync();
            //3.Mybatis 实现批量新增
         if (dao.saveBatch(list)>0){
             //4.返回
             return R.ok();
         }
        }
        return R.fail();
    }

    @Override
    public R delIds(int[] ids) {
        if (dao.delByIds(ids)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    //根据菜id进行分类统计
    @Override
    public R kindTj() {
//        //1.查询数据
//        List<ct_OrderDetailNameTjDto> list=dao.kindTj();
//        System.err.println("*************");
//        System.err.println(list);
////        List<ct_OrderDetailNameTjDto> result=new ArrayList<>();
//        //2.组装数据  JDk8新特性
//        list.forEach(s->{
//           list.add(s.getValue(),s.getName());
//        });
        System.err.println(dao.kindTj());
        System.err.println("******");
        return R.ok(dao.kindTj());
    }
}
